;
! function() {
	var $ = layui.jquery,
		layer = layui.layer,
		form = layui.form;

	refCode();

	// 点击刷新验证码
	$("#addAdmin_code_img").on("click", function() {
		refCode();
	});

	// 获取验证码
	function refCode() {
		$.ajax({
			url: "/secadminuser/getPictureVerificationCode",
			success: function(result) {
				$("#addAdmin_code_img").prop("src",
					"data:image/jpg;base64," + result.image);
			}
		});
	}

	// 自定义表单验证
	form.verify({
		userName: function(value) {
			if(value.length < 3) {
				return "用户名长度不能少于三位";
			} else if(!/^(?!^\d+$)[0-9a-zA-Z]{3,10}$/.test(value)) {
				return "用户名只能包含纯字符，或者数字和字符混合";
			}
		},
		pwd: function(value) {
			if(value.length < 6) {
				return "密码长度不能少于6位";
			} else if(!/^(\w){6,12}$/.test(value)) {
				return "密码只能包含字母、数字或下划线";
			}
		},
		newPwd: function(value) {
			if(value != $("#admin-pwd").val()) {
				return "两次输入的密码不一致";
			}
		},
		code: function(value) {
			if(value.length < 6) {
				return "验证码长度不能少于6位";
			}
		}
	});

	form.on("submit(addAdmin)", function() {
		$.ajax({
			url: "/secadminuser/addAdminUser",
			type: "post",
			data: {
				"userName": $("#admin-userName").val(),
				"pwd": $("#admin-pwd").val(),
				"code": $("#admin-code").val()
			},
			async: false,
			success: function(result) {
				if(result.status == 0) {
					layer.msg(result.msg);
					$("input").val("");
					kp();
				} else {
					layer.msg(result.msg, {
						icon: 5
					});
				}
			}
		});
		return false;
	});

	// 输入密码新密码时实时显示密码强度
	$("#admin-pwd").keyup(function() {
		kp();
	});

	$("#cz").click(function() {
		kp();
	});

	function kp() {
		var val = $("#admin-pwd").val();
		var num = checkStrong(val);
		switch(num) {
			case 0:
				$("#checkPwdSecurity").children().css({
					'background': 'lightgray',
					"color": "gray"
				});
				break;
			case 1:
				$("#checkPwdSecurity").children().css({
					'background': 'lightgray',
					"color": "gray"
				});
				$($("#checkPwdSecurity").children("div").get(0)).css({
					'background': 'red',
					"color": "black"
				});
				break;
			case 2:
				$("#checkPwdSecurity").children().css({
					'background': 'lightgray',
					"color": "gray"
				});
				$($("#checkPwdSecurity").children("div").get(1)).css({
					'background': 'yellow',
					"color": "black"
				});
				break;
			case 3:
				$("#checkPwdSecurity").children().css({
					'background': 'lightgray',
					"color": "gray"
				});
				$($("#checkPwdSecurity").children("div").get(1)).css({
					'background': 'yellow',
					"color": "black"
				});
				break;
			case 4:
				$("#checkPwdSecurity").children().css({
					'background': 'lightgray',
					"color": "gray"
				});
				$($("#checkPwdSecurity").children("div").get(2)).css({
					'background': 'green',
					"color": "black"
				});
				break;
			default:
				break;
		}
	}

	// 检测密码强度
	function checkStrong(val) {
		var modes = 0;
		if(val.length < 5)
			return 0;
		if(/\d/.test(val))
			modes++; // 数字
		if(/[a-z]/.test(val))
			modes++; // 小写
		if(/[A-Z]/.test(val))
			modes++; // 大写
		if(/\W/.test(val))
			modes++; // 特殊字符
		if(val.length > 12)
			return 4;
		return modes;
	};
}();